<?php
require_once('Extension/Controller/Plugin/Abstract.php');

class Extension_Controller_Plugin_Web_Auth_Http extends Extension_Controller_Plugin_Abstract
{
    //public function routeStartup(Zend_Controller_Request_Abstract $request) {}
    //public function routeShutdown(Zend_Controller_Request_Abstract $request) {}

    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    {
        self::initAuthHttp($request);
    }

    //public function preDispatch(Zend_Controller_Request_Abstract $request) {}
    //public function postDispatch(Zend_Controller_Request_Abstract $request) {}
    //public function dispatchLoopShutdown() {}


    public static function initAuthHttp(Zend_Controller_Request_Abstract $request)
    {
        $config = Kbs::initConfig('Auth/Http');

        if ($config->enable) {

            require_once('Zend/Auth.php');
            $auth = Zend_Auth::getInstance();

            $namespace = Kbs::initConfig('Session')->namespace->auth->http;

            require_once('Extension/Auth/Storage/Session.php');
            $storage = new Extension_Auth_Storage_Session($namespace);
            $auth->setStorage($storage);

            if (!$auth->hasIdentity()) {
  
                require_once('Zend/Auth/Adapter/Http/Resolver/File.php');
                $basicResolver = new Zend_Auth_Adapter_Http_Resolver_File();
                $basicResolver->setFile( PROJECT_ROOT . $config->resolver->basic );
                
                //$digestResolver = new Zend_Auth_Adapter_Http_Resolver_File();
                //$digestResolver->setFile( PROJECT_ROOT . $config->resolver->digest);

                require_once('Zend/Auth/Adapter/Http.php');
                $authAdapterHttp = new Zend_Auth_Adapter_Http($config->options->toArray());
                //$authAdapterHttp->setDigestResolver($digestResolver);
                $authAdapterHttp->setBasicResolver($basicResolver);

                //$request = Zend_Controller_Front::getInstance()->getRequest();
                $response = Zend_Controller_Front::getInstance()->getResponse();

                //assert($request instanceof Zend_Controller_Request_Http); 
                assert($response instanceof Zend_Controller_Response_Http);

                $authAdapterHttp->setRequest($request);
                $authAdapterHttp->setResponse($response);

                Zend_Registry::set('authAdapterHttp', $authAdapterHttp);
                //return $authAdapterHttp;

                $auth = Zend_Auth::getInstance();
                $result = $auth->authenticate($authAdapterHttp);

                if (!$result->isValid()) {
                    //echo md5('kim') . '<br />';
                    //echo base64_encode('kim') . '<br />';
                    //file_put_contents(PROJECT_ROOT . $config->resolver->digest, 'kim:site:'.md5('kim'));

                    echo $authAdapterHttp->getResponse();
                    echo $messages = implode('<br />', $result->getMessages());
                    exit();
                }

            }

        } else {
            return false;
        }
    }
}